package com.bicon.botu.mall.base.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.mall.base.dto.LogisticsCenterDto;
import com.bicon.botu.mall.base.entity.LogisticsCenter;
import com.bicon.botu.mall.base.service.ILogisticsCenterService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther 裴志鹏
 * @date 2017/9/8 10:57
 */
@RestController
@RequestMapping("/V1.0.0/logisticsCenter")
public class LogisticsCenterController extends BaseController {

    @Autowired
    private ILogisticsCenterService iLogisticsCenterService;

    /**
     * 新增物流信息
     *
     * @param logisticsCenterJson 物流信息Json
     * @return
     */
    @RequestMapping(value = "/add")
    public BaseResult add(@RequestParam String logisticsCenterJson) {
        return iLogisticsCenterService.add(logisticsCenterJson);
    }

    /**
     * 修改物流信息
     *
     * @param logisticsCenterJson 物流信息Json
     * @return
     */
    @RequestMapping(value = "/update")
    public BaseResult update(@RequestParam String logisticsCenterJson) {
        return iLogisticsCenterService.update(logisticsCenterJson);
    }

    /**
     * 删除物流信息
     *
     * @param id 物流信息id
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String id) {
        LogisticsCenter logisticsCenter = new LogisticsCenter();
        logisticsCenter.preUpdate();
        logisticsCenter.setLogicDelete(Constants.DEL_LOGIC_DELETE);
        logisticsCenter.setId(id);
        boolean flag = logisticsCenter.updateById();
        if (!flag) {
            return sendResult500();
        }
        return sendResult200();
    }

    /**
     * 分页查询物流信息
     *
     * @param current 当前页
     * @param size    每页显示条数
     * @return
     */
    @RequestMapping(value = "/findByPaging")
    public BaseResult findByPaging(@RequestParam int current, @RequestParam int size) {
        Page<LogisticsCenter> logisticsCenterPage = new LogisticsCenter().selectPage(new Page<>(current, size), new EntityWrapper<LogisticsCenter>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("create_date", false));
        return sendResult200(logisticsCenterPage);
    }

    /**
     * 查询所有物流的名称
     *
     * @return
     */
    @RequestMapping(value = "/findAllName")
    public BaseResult findAllName() {
        List<LogisticsCenter> logisticsCenters = new LogisticsCenter().selectList(new EntityWrapper<LogisticsCenter>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).eq("enable","0"));
        List<LogisticsCenterDto> logisticsCenterDtos = BeanMapper.mapList(logisticsCenters, LogisticsCenterDto.class);
        return sendResult200(logisticsCenterDtos);
    }
}
